﻿using System.Linq;
using System.Web.Mvc;
using ComeSeeThis_MVC2.Models;

namespace ComeSeeThis_MVC2.Controllers
{
    public class TireController : Controller
    {
        readonly TireRepository _tireRepository = new TireRepository();
        //
        // GET: /Tire/

        #region Get Methods

        public ActionResult Index(int page = 0, int pageSize = 10)
        {
            //const int pageSize = 10;
            var tires = _tireRepository.GetAllTires();//.OrderBy(v => v.Company.CompanyName).OrderBy((v => v.TireName));

            var paginatedTires = new PaginatedList<Tire>(tires, page, pageSize);

            return View(tires);
        }

        public ActionResult Details(int id)
        {
            var tire = _tireRepository.GetTire(id);

            return tire == null ? View("TireNotFound") : View(tire);
        }

        #endregion

        #region Edit Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Edit(int id)
        {
            var tire = _tireRepository.GetTire(id);

            return View(new TireFormView(tire));
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            var tire = _tireRepository.GetTire(id);

            foreach (var formValue in formValues)
            {
                formValue.ToString();
            }

            if (TryUpdateModel(tire, "Tire"))
            {
                _tireRepository.Save();
                return RedirectToAction("Details", new { id = tire.TireId });
            }

            return View(new TireFormView(tire));
        }

        #endregion

        #region Create Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Create()
        {
            var tire = new Tire();

            return View(new TireFormView(tire));
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Create(Tire tire, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                // Update the vehicle types
                _tireRepository.Add(tire);
                _tireRepository.Save();

                var newTire = _tireRepository.GetLastTire();
                return View("Edit", new TireFormView(newTire));
                //return RedirectToAction("Edit", newTire.TireId);
            }
            return View(new TireFormView(tire));
        }

        [HttpPost, Authorize]
        public ActionResult SubmitVehicleType(int tireId, int vehicleTypeId)
        {
            if (Request.IsAjaxRequest())
            {
                var context = new ComeSeeThisDBEntities();
                var tire = context.Tires.Where(x => x.TireId == tireId).First();
                var vehicleType = context.VehicleTypes.Where(x => x.VehicleTypeId == vehicleTypeId).First();

                tire.VehicleTypes.Add(vehicleType);

                context.SaveChanges();

                return View("VehicleTypeList", new VehicleTypeTireFormView(tireId));
            }
            return View("Edit", tireId);

        }

        #endregion

        #region Delete Methods

        [Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Delete(int id)
        {
            var tire = _tireRepository.GetTire(id);

            return tire == null ? View("TireNotFound") : View(tire);
        }

        [HttpPost, Authorize]//(Roles = "SiteAdmin,GearingAdmin,ContentAdmin")]
        public ActionResult Delete(int id, string confirmButton)
        {
            var tire = _tireRepository.GetTire(id);

            if (tire == null) return View("TireNotFound");

            _tireRepository.Delete(tire);
            _tireRepository.Save();

            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult DeleteVehicleType(int id, int vehicleTypeId)
        {
            if (Request.IsAjaxRequest())
            {
                var context = new ComeSeeThisDBEntities();
                var tire = context.Tires.Where(x => x.TireId == id).First();
                var vehicleType = context.VehicleTypes.Where(x => x.VehicleTypeId == vehicleTypeId).First();

                tire.VehicleTypes.Remove(vehicleType);

                context.SaveChanges();

                return View("VehicleTypeList", new VehicleTypeTireFormView(id));
            }
            return View("Edit", id);
        }
        #endregion
    }
}
